package com.study.animationstudy.ui

import android.animation.Animator
import android.animation.Animator.AnimatorListener
import android.animation.ArgbEvaluator
import android.animation.FloatEvaluator
import android.animation.ObjectAnimator
import android.animation.RectEvaluator
import android.graphics.Color
import android.graphics.Rect
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.view.animation.AccelerateInterpolator
import android.view.animation.BounceInterpolator
import android.view.animation.DecelerateInterpolator
import android.view.animation.LinearInterpolator
import android.widget.AdapterView
import android.widget.AdapterView.OnItemSelectedListener
import android.widget.ArrayAdapter
import com.study.animationstudy.R
import com.study.animationstudy.base.BaseActivity
import com.study.animationstudy.databinding.ActivityInterpolatorBinding

class InterpolatorActivity : BaseActivity<ActivityInterpolatorBinding>(),AnimatorListener {
    // 声明四个属性动画对象
    private lateinit var animAcce:ObjectAnimator
    private lateinit var animDece:ObjectAnimator
    private lateinit var animLinear:ObjectAnimator
    private lateinit var animBounce:ObjectAnimator

    private val interpolatorArray = arrayOf("背景色+加速插值器+颜色估值器", "旋转+减速插值器+浮点型估值器",
        "裁剪+匀速插值器+矩形估值器", "文字大小+震荡插值器+浮点型估值器")

    override fun ActivityInterpolatorBinding.initBinding() {
        //初始化属性动画
        initObjectAnim()
        //初始化插值器类型的下拉框
        initInterpolatorSpinner()
    }

    /**
     * 初始化插值器类型的下拉框
     */
    fun initInterpolatorSpinner(){
        val interpolatorAdapter = ArrayAdapter(this,R.layout.item_select,interpolatorArray)
        mBinding.spInterpolator.apply {
            prompt = "请选择插值器类型"
            adapter = interpolatorAdapter
            onItemSelectedListener = object : OnItemSelectedListener{
                override fun onItemSelected(
                    parent: AdapterView<*>?,
                    view: View?,
                    position: Int,
                    id: Long
                ) {
                    // 根据插值器类型展示属性动画
                    showInterpolator(position)
                }

                override fun onNothingSelected(parent: AdapterView<*>?) {

                }

            }
        }
    }

    //初始化属性动画
    fun initObjectAnim(){
        //构造一个在背景色上变化的属性动画
        animAcce = ObjectAnimator.ofInt(mBinding.tvInterpolator,"backgroundColor",Color.RED,Color.GRAY)
        //给属性动画设置加速插值器
        animAcce.interpolator = AccelerateInterpolator()
        //给属性动画设置颜色估值器
        animAcce.setEvaluator(ArgbEvaluator())

        //构造一个围绕中心点旋转的属性动画
        animDece = ObjectAnimator.ofFloat(mBinding.tvInterpolator,"rotation",0f,360f)
        //给属性动画设置减速插值器
        animDece.interpolator = DecelerateInterpolator()
        //给属性动画设置浮点型估值器
        animDece.setEvaluator(FloatEvaluator())

        //构造一个在文字大小上变化的属性动画
        animBounce = ObjectAnimator.ofFloat(mBinding.tvInterpolator,"textSize",20f,60f)
        //给属性动画设置震荡插值器
        animBounce.interpolator = BounceInterpolator()
        //给属性动画设置浮点型估值器
        animBounce.setEvaluator(FloatEvaluator())
    }


    /**
     * 根据插值器类型展示属性动画
     */
    fun showInterpolator(type:Int){
        var anim:ObjectAnimator? = null
        when(type){
            // 背景色+加速插值器+颜色估值器
            0 -> anim = animAcce
            // 旋转+减速插值器+浮点型估值器
            1 -> anim = animDece
            // 裁剪+匀速插值器+矩形估值器
            2 -> {
                val width = mBinding.tvInterpolator.width
                val height = mBinding.tvInterpolator.height
                // 构造一个从四周向中间裁剪的属性动画，同时指定了矩形估值器RectEvaluator
                animLinear = ObjectAnimator.ofObject(mBinding.tvInterpolator,"clipBounds",
                    RectEvaluator(),Rect(0,0,width,height),
                    Rect(width/3,height/3,width/3*2,height/3*2),
                    Rect(0,0,width,height))
                // 给属性动画设置匀速插值器
                animLinear.interpolator = LinearInterpolator()
                anim = animLinear
            }
            // 文字大小+震荡插值器+浮点型估值器
            3 -> {
                anim = animBounce
                // 给属性动画添加动画事件监听器。目的是在动画结束时恢复文字大小
                anim.addListener(this)
            }
        }
        // 设置动画的播放时长
        anim?.duration = 2000
        anim?.start()
    }

    /**
     * 在属性动画开始播放时触发
     * @param animation Animator
     */
    override fun onAnimationStart(animation: Animator?) {

    }

    override fun onAnimationEnd(animation: Animator?) {
        if (animation?.equals(animBounce) == true){
            // 构造一个在文字大小上变化的属性动画
            val anim = ObjectAnimator.ofFloat(mBinding.tvInterpolator,"textSize",60f,20f)
            // 给属性动画设置震荡插值器
            anim.interpolator = BounceInterpolator()
            // 给属性动画设置浮点型估值器
            anim.setEvaluator(FloatEvaluator())
            anim.duration = 2000
            anim.start()
        }
    }

    override fun onAnimationCancel(animation: Animator?) {

    }

    override fun onAnimationRepeat(animation: Animator?) {

    }

}